package com.ecube.maintenance.components.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ecube.maintenance.utils.ListUtil;

import java.util.List;


public class AutoSwitchText extends TextView {
    private Context mContext;
    private Handler mHandler;
    private Runnable mRunnable;
    private int counter = 0;
//    private String[] text = new String[]{
//            "手机用户186-***-58546预定了2014-09-19 AM 10:00 工位",
//            "手机用户187-***-51245预定了2014-09-19 PM 4:00 工位",
//            "手机用户136-***-48781预定了2014-09-20 AM 9:00 工位"
//    };
    List<String> text;

    public AutoSwitchText(Context context) {
        super(context);
        this.mContext = context;
        drawView();
    }

    public AutoSwitchText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        drawView();
    }

    public AutoSwitchText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        drawView();
    }

    private void drawView() {
        if (ListUtil.isEmpty(text))return;
        mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                ((TextView) AutoSwitchText.this).setText(text.get(counter % text.size()));
                counter++;
                postDelayed(mRunnable, 5 * 1000);
            }
        };
        mHandler.post(mRunnable);
    }

    public void removeCallBack() {
        if ( mHandler != null )
        mHandler.removeCallbacks(mRunnable);
    }
    public void setTexts(List<String> texts){
        text = texts;
        drawView();
    }
}
